<?php

namespace App\Http\Controllers\Market;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\MarketExpense;
use Illuminate\Http\Request;

class MarketExpenseController extends Controller
{
    use ResponseData;
    public function index(MarketExpense $service,Request $request){
        $size = $request->input('size',10);
        if($request->filled('start')&&$request->filled('end')){
            $service = $service->whereBetween('created_at',[$request->start,$request->end]);
        }
        if($request->filled('plateform_name')){
            $service = $service->where('plateform_name', 'like','%'.$request->plateform_name.'%');
        }
        if($request->filled('type')){
            $service = $service->where('type',$request->type);
        }
        $data = $service->orderBy('id','desc')->paginate($size);
        $totalsum = $service->selectRaw('sum(amount) as mount_all')->first();
        $amount_all = $totalsum->mount_all;
        // return $this->success($data);
        return $this->success(compact('data','amount_all'));
    }
    public function store(MarketExpense $ser,Request $request){
        $data = $ser->create($request->all());
        return $this->success($data);
    }
    public function edit(MarketExpense $service,Request $request){
        $data = $service->find($request->id);
        return $this->success($data);
    }
    public function update(MarketExpense $service,Request $request){
        $model = $service->find($request->id);
        $data = $request->all();
        $model->update($data);
        return $this->success('更新成功');
    }

    public function destory(MarketExpense $service,Request $request){
        $model = $service->find($request->id);
        $model->delete();
        return $this->success('删除成功');
    }
}
